package com.example.javaapitest.common;

import bsh.EvalError;
import bsh.Interpreter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
public class RunBeanShell {
    public static void runShell(String string) throws EvalError, IOException {
        Interpreter interpreter = new Interpreter();

        Map n = new HashMap();
        n.put("x","y");
        interpreter.set("inValue", n);

        interpreter.eval(string);
        //interpreter.source("e:/testScript.txt");
        //打印计算结果
        System.out.println(interpreter.get("outValue"));
    }

    private static Map getTime(String inValue){
        Map m = new HashMap();
        UUID uuid1 = UUID.randomUUID();
        m.put("a",uuid1);
        m.put("b",inValue);
        m.get("a");
        System.out.println(m.toString());
        return m;
    }

    public static void main(String[] args) throws EvalError, IOException {
        String evalString = "import java.util.*;\n" +
                "\n" +
                "private Map getMap(String inValue){\n" +
                "        Map m = new HashMap();\n" +
                "        UUID uuid1 = UUID.randomUUID();\n" +
                "        m.put(\"a\",uuid1);\n" +
                "        m.put(\"b\",inValue);\n" +
                "        return m;\n" +
                "}\n" +
                "Map outValue = getMap(inValue.get(\"x\"));\n" +
                "System.out.println(outValue.get(\"a\")+\"---\"+outValue.get(\"b\"));";

        //调用beanshell进行计算
        runShell(evalString);
    }
}
